package com.zhuchaooasystem.presenter.home.servicedetail;

import android.app.Activity;
import android.util.Log;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.FinishServiceEntity;
import com.zhuchaooasystem.entity.ServiceCardsInfoEntity;
import com.zhuchaooasystem.entity.ServiceDetailEntity;
import com.zhuchaooasystem.entity.UserInfo;

import java.util.HashMap;
import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by zhangyajiang on 2017/1/3.
 */

public class ServiceDetailPresenter extends BasePresenter<ServiceDetailContract.View> implements ServiceDetailContract.Presenter {

    public ServiceDetailPresenter(Activity activity, ServiceDetailContract.View view) {
        super(activity, view);
    }

    @Override
    public void downServiceDetailData(final String id) {
        Map<String, String> map = new HashMap<>();
        map.put("serviceid", id);
        Log.d("ServiceDetailPresenter", "id:"+id);
        showLoading();
        Subscription subscription = HttpFactory.getHttpApi().getServiceDetail(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        ServiceDetailEntity entity = (ServiceDetailEntity) o;
                        if (entity != null) {
                            if (null != entity.getResult().getSuccessful()) {
                                mView.showAnonymousData(entity.getResult().getSuccessful());
                                mView.showServiceData(entity.getResult().getSuccessful());
                                mView.showCardsInfoData(entity.getResult().getSuccessful());
                                mView.showMaterialData(entity.getResult().getSuccessful());
                                mView.showExceptionData(entity.getResult().getSuccessful());

                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                        LoadingDiaogDismiss();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LoadingDiaogDismiss();
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    /**
     * 下载签到数据
     *
     * @param serviceid
     */
    @Override
    public void finishService(int serviceid) {
        mView.setClickable(false);
        int userId = UserInfo.getInstance().getMemberId();
        String username = UserInfo.getInstance().getUserName();
        Map<String, String> map = new HashMap<>();
        map.put("serviceid", serviceid + "");
        map.put("memberId", userId + "");
        map.put("UserName", username);

        Subscription subscription = HttpFactory.getHttpApi().accomplishServiceFlow(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            FinishServiceEntity entity = (FinishServiceEntity) o;
                            if (entity != null) {
                                if (null != entity.getResult().getSuccessful()) {
                                    mView.finishService();
                                } else if (null != entity.getResult().getError()) {
                                    handleError(entity.getResult().getError());
                                }
                            }
                        }
                        mView.setClickable(true);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        handleError(throwable);
                        mView.setClickable(true);
                        LoadingDiaogDismiss();
                    }
                });
        addSubscrebe(subscription);
    }

    /**
     * 下载签到数据
     *
     * @param id
     */
    @Override
    public void downServiceCardsInfo(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("serviceid", id);

        Subscription subscription = HttpFactory.getHttpApi().getServiceCardsInfo(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        ServiceCardsInfoEntity entity = (ServiceCardsInfoEntity) o;

                        if (entity != null) {
                            //    mView.showCardsInfoData(entity.getResult().getSuccessful());
                        }


                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }
}
